<?php

// @formatter:off
/**
 * @file ct.previews.update.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: ct.previews.update.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/cron/ct.previews.update.php $
 */
// @formatter:on

class WCUpdatePreviews extends WCCronTool {
	//
	// Constants.
	const MaxPerRunTags = 500;
	const MaxPerRunRules = 500;
	//
	// Public methods.
	public function run($spacer = "") {
		//
		// Loading global pointers.
		global $wcOptions;

		$checkPaths = $wcOptions->activated("CheckPaths");
		$groupId = $wcOptions->activated("Group") ? $wcOptions->value("Group") : false;

		$done = false;
		$doneCount = 1;
		foreach(WCGroupsHolder::I()->items() as $group) {
			//
			// Ignoring not matching groups when there's a specified
			// one.
			if($groupId && $group->id != $groupId) {
				continue;
			}

			if(is_readable($group->root())) {
				echo "{$spacer}Checking group '{$group}':\n";

				if($checkPaths) {
					$this->checkPaths($group, "{$spacer}\t");
				}

				$this->updateDirectoryItems($group, $done, $doneCount, "{$spacer}\t");
				if($done) {
					break;
				}
				$this->updateDirectories($group, "{$spacer}\t");
				$this->updateTags($group, "{$spacer}\t");
				$this->updateCollections($group, "{$spacer}\t");
				$this->updateRules($group, "{$spacer}\t");
			}
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}This tool seeks for any comic, collection, tag or directory that seems to have not a valid preview.\n";

		echo "\n";

		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-c, --check-paths\n";
		echo "{$spacer}\t\tCheck if preview's paths are readable.\n\n";
		echo "{$spacer}\t-g group-id, --group group-id\n";
		echo "{$spacer}\t\tIt only run active tags for an specific group.\n\n";
	}
	//
	// Protected methods.
	protected function checkPaths(WCGroup &$group, $spacer = "") {
		echo "{$spacer}Checking invalid preview paths:";
		$invalids = WCPreviewsManager::I()->checkAllPaths($group);
		if(count($invalids)) {
			echo "\n";
			foreach($invalids as $invalid) {
				echo "{$spacer}\tPath '{$invalid}' marked as invalid.\n";
			}
		} else {
			echo WCColorText::ShellGreen(" Done\n");
		}
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addValueOption("Group")) {
				$wcOptions->addOptionTrigger("Group", "g");
				$wcOptions->addOptionTrigger("Group", "group");
			}
			if($wcOptions->addOption("CheckPaths")) {
				$wcOptions->addOptionTrigger("CheckPaths", "c");
				$wcOptions->addOptionTrigger("CheckPaths", "check-paths");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
	protected function updateCollections(WCGroup &$group, $spacer = "") {
		foreach(WCCollectionsHolder::I()->itemsByGroup($group) as $collection) {
			if(($collection->count > 0 || $collection->icount > 0 || $collection->subcount > 0) && !$collection->hasValidPreviews()) {
				echo "{$spacer}Generating previews for collection {$collection}: ";
				if($collection->generatePreviews()) {
					echo WCColorText::ShellGreen("Done")."\n";
				} else {
					echo WCColorText::ShellRed("Failed")."\n";
				}
			}
		}
	}
	protected function updateDirectories(WCGroup &$group, $spacer = "") {
		foreach(WCDirectoriesHolder::I()->itemsByGroup($group) as $directory) {
			if(($directory->count > 0 || $directory->icount > 0) && !$directory->hasValidPreviews()) {
				echo "{$spacer}Generating previews for directory {$directory}: ";
				if($directory->generatePreviews()) {
					echo WCColorText::ShellGreen("Done")."\n";
				} else {
					echo WCColorText::ShellRed("Failed")."\n";
				}
			}
		}
	}
	protected function updateDirectoryItems(WCGroup &$group, &$done, &$doneCount, $spacer = "") {
		global $wcDefaults;

		foreach(WCDirectoriesHolder::I()->itemsByGroup($group) as $directory) {
			foreach($directory->comicsFull() as $comic) {
				$incrementDone = false;

				if($comic->ok() && !$comic->hasValidPreviews()) {
					echo "{$spacer}Checking {$comic}:\n";

					foreach($comic->images() as $cimage) {
						if(!$cimage->hasValidPreviews()) {
							$incrementDone = true;

							echo "{$spacer}\tGenerating preview '{$cimage}': ";
							if($cimage->generatePreviews()) {
								echo WCColorText::ShellGreen("Done")."\n";
							} else {
								echo WCColorText::ShellRed("Failed")."\n";
								$ok = false;
							}
						}
					}

					if(!$comic->hasValidPreviews()) {
						echo "{$spacer}\tGenerating comic preview: ";
						if($comic->generatePreviews()) {
							echo WCColorText::ShellGreen("Done")."\n";
						} else {
							echo WCColorText::ShellRed("Failed")." (path: {$comic->path()})\n";
							$ok = false;
						}
					}

					$comic->shrink();
				} else {
					
				}

				$doneCount += $incrementDone ? 1 : 0;

				$done = $wcDefaults["max-autoexpand"] < $doneCount;

				if($done) {
					break;
				}
			}

			foreach($directory->imagesFull() as $image) {
				if($image->ok() && !$image->hasValidPreviews()) {
					echo "{$spacer}Checking {$image}:\n";
					$ok = true;

					if(!$image->hasValidPreviews()) {
						echo "{$spacer}\tGenerating image preview: ";
						if($image->generatePreviews()) {
							echo WCColorText::ShellGreen("Done")."\n";
						} else {
							echo WCColorText::ShellRed("Failed")." (path: {$image->path()})\n";
							$ok = false;
						}
					}
				}
			}

			if($done) {
				break;
			}
		}
	}
	protected function updateRules(WCGroup &$group, $spacer = "") {
		$runCount = self::MaxPerRunRules;
		foreach(WCRulesHolder::I()->itemsByGroup($group) as $rule) {
			if(($rule->count > 0 || $rule->icount > 0) && !$rule->hasValidPreviews()) {
				echo "{$spacer}Generating previews for rule {$rule}: ";
				if($rule->generatePreviews()) {
					echo WCColorText::ShellGreen("Done")."\n";
				} else {
					echo WCColorText::ShellRed("Failed")."\n";
				}
			}

			$runCount--;
			if(!$runCount) {
				break;
			}
		}
	}
	protected function updateTags(WCGroup &$group, $spacer = "") {
		$runCount = self::MaxPerRunTags;
		foreach(WCTagsHolder::I()->itemsByGroup($group) as $tag) {
			if(($tag->count > 0 || $tag->icount > 0) && !$tag->hasValidPreviews()) {
				echo "{$spacer}Generating previews for tag {$tag}: ";
				if($tag->generatePreviews()) {
					echo WCColorText::ShellGreen("Done")."\n";
				} else {
					echo WCColorText::ShellRed("Failed")."\n";
				}
			}

			$runCount--;
			if(!$runCount) {
				break;
			}
		}
	}
}
